package com.itheima.fifth.service.impl;

import com.carkeeper.storebill.pojo.StoreBill;
import com.itheima.fifth.dao.HomepageMapper;
import com.itheima.fifth.service.HomepageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class HomepageServiceImpl implements HomepageService {

    @Autowired
    private HomepageMapper homepageMapper;
    @Override
    public StoreBill findById(String id) {
        return (StoreBill) homepageMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StoreBill> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return homepageMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(StoreBill.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andLike("id","%"+searchMap.get("id")+"%");
            }
            if(searchMap.get("code")!=null && !"".equals(searchMap.get("code"))){
                criteria.andLike("code","%"+searchMap.get("code")+"%");
            }
            if(searchMap.get("store_id")!=null && !"".equals(searchMap.get("store_id"))){
                criteria.andLike("storeId","%"+searchMap.get("store_id")+"%");
            }
            if(searchMap.get("store_name")!=null && !"".equals(searchMap.get("store_name"))){
                criteria.andLike("storeName","%"+searchMap.get("store_name")+"%");
            }
            if(searchMap.get("price")!=null && !"".equals(searchMap.get("price"))){
                criteria.andLike("price","%"+searchMap.get("price")+"%");
            }
            if(searchMap.get("consign_status")!=null && !"".equals(searchMap.get("consign_status"))){
                criteria.andLike("consignStatus","%"+searchMap.get("consign_status")+"%");
            }
            if(searchMap.get("accounts_code")!=null && !"".equals(searchMap.get("accounts_code"))){
                criteria.andLike("accountsCode","%"+searchMap.get("accounts_code")+"%");
            }
        }
        return example;
    }



}
